package com.cby.jetpack.sample.binding.widget

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.widget.SeekBar
import androidx.databinding.*
import java.util.jar.Attributes

/**
 * @description: CbySeekbar
 *
 * @author ChenBaoyang
 * @date 8/10/21 17:19
 */
//@InverseBindingMethods([@InverseBindingMethod(type = CbySeekBar::class.java, attribute = "cbyProgress", event = "cbyProgressAttrChanged", method = "getCbyProgress")])
class CbySeekBar(context: Context?, attrs: AttributeSet?) : SeekBar(context, attrs){

    companion object{
        var mInverseBindingListener: InverseBindingListener? = null

        @BindingAdapter("cbyProgress")
        @JvmStatic
        fun setCbyProgress(seekBar: CbySeekBar , progress: Int) {
            if (getCbyProgress(seekBar) != progress) {
                seekBar.progress = progress
            }
        }

        @InverseBindingAdapter(attribute = "cbyProgress", event = "cbyProgressAttrChanged")
        @JvmStatic
        fun getCbyProgress(seekBar: CbySeekBar) : Int {
            return seekBar.progress
        }

        @BindingAdapter("cbyProgressAttrChanged")
        @JvmStatic
        fun setCbyProgressAttrChanged(seekBar: CbySeekBar , inverseBindingListener:InverseBindingListener?) {
            mInverseBindingListener = inverseBindingListener
        }
    }

    init{
        this.setOnSeekBarChangeListener(object :OnSeekBarChangeListener {

            override fun onProgressChanged(seekBar:SeekBar , i:Int , b:Boolean ) {
                //触发反向数据的传递
                mInverseBindingListener?.onChange()
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {

            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {

            }

        });
    }
}